package com.lzl.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzl.eduservice.entity.EduSubject;
import com.lzl.eduservice.entity.excel.SubjectData;
import com.lzl.eduservice.entity.subject.OneSubject;
import com.lzl.eduservice.entity.subject.TwoSubject;
import com.lzl.eduservice.listener.SubjectExcelListener;
import com.lzl.eduservice.mapper.EduSubjectMapper;
import com.lzl.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-03-29
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    //添加课程分类
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService eduSubjectService) {
        try {
            //文件输入流
            InputStream inputStream = file.getInputStream();
            //调用方法进行读取
            //在你newSubjectExcelListener
            //在你newSubjectExcelListener对象时，把eduSubjectService参数传过去
            EasyExcel.read(inputStream, SubjectData.class,new SubjectExcelListener(eduSubjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //课程分类列表（树形）
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //查询到所以的一级分类 parent_id = 0
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id",0);
        //baseMapper在继承的ServiceImpl中是自动注入的
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);

        //查询到所有的二级分类 parent_id != 0
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id",0);
        //baseMapper在继承的ServiceImpl中是自动注入的
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTwo);

        //创建list集合，用户存储最终封装的数据
        List<OneSubject> finalSubjectList = new ArrayList<>();

        //封装一级分类
        //查询出来所以的一级分类集合遍历，得到每一个分类对象，获取每一个分类对象的值
        //把值封装到要求的finalSubjectList里
        for (EduSubject eduSubject : oneSubjectList) {
            OneSubject oneSubject = new OneSubject();
//            oneSubject.setId(eduSubject.getId());
//            oneSubject.setTitle(eduSubject.getTitle());
            //如果属性多，就很麻烦，现在使用工具类 第一个参数 是get的参数  第二个是set的参数
            //自动根据属性赋值
            BeanUtils.copyProperties(eduSubject,oneSubject);


            //封装到二级分类
            //在一级分类循环遍历查询所有的二级分类
            //创建list集合封装每个一级分类的二级分类
            List<TwoSubject> twofinalSubjectList = new ArrayList<>();
            for (EduSubject subject : twoSubjectList) {
                //不能用== 来判断
                if(subject.getParentId().equals(eduSubject.getId())){
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(subject,twoSubject);
                    twofinalSubjectList.add(twoSubject);
                }
            }
            oneSubject.setChildren(twofinalSubjectList);
            finalSubjectList.add(oneSubject);
        }
        return finalSubjectList;
    }
}
